<?php
function perb_add($karyawanPelaku, $lapor_id, $perb_tgl, $perb_waktu, $perb_privileges, $perb_point){
	$sql = "INSERT INTO perbuatan VALUES ('', '$karyawanPelaku', '$lapor_id', '$perb_tgl', '$perb_waktu', '$perb_privileges', '$perb_point')";
	return mysql_query($sql);
}

function perb_update($perbNO, $karyawanPelaku=false, $lapor_id=false, $perb_tgl=false, $perb_waktu=false, $perb_privileges=false, $perb_point=false){
	$sqls = "";
	if ($karyawanPelaku) 	$sqls .= "KODE_KARYAWAN='$karyawanPelaku'";
	if ($lapor_id)			$sqls .= ($sqls? "," : "")."LAPOR_ID='$lapor_id'";
	if ($perb_tgl)			$sqls .= ($sqls? "," : "")."PERB_TGL='$perb_tgl'";
	if ($perb_waktu)		$sqls .= ($sqls? "," : "")."PERB_WAKTU='$perb_waktu'";
	if ($perb_privileges)	$sqls .= ($sqls? "," : "")."PERB_PRIVILEGES='$perb_privileges'";
	if ($perb_point)		$sqls .= ($sqls? "," : "")."PERB_POINT='$perb_point'";

	$sqlw= "PERB_NO='$perbNO'";
	return mysql_query("UPDATE perbuatan SET $sqls WHERE $sqlw");
}

function perb_load($perbNO){
	$sql = "SELECT * FROM perbuatan WHERE PERB_NO='$perbNO'";
	return mysql_query($sql);
}

function perb_load_last(){
	$sql = "SELECT MAX(PERB_NO) as PERB_NO FROM perbuatan";
	return mysql_query($sql);
}

function perb_select($key=false, $where=false, $orbderby=false, $groupby=false){
	$sql = "SELECT * FROM perbuatan as a ". ($key||$where? " WHERE " : "");
	if ($key){
		$sql .= "(a.PERB_NO	LIKE '%$key%' OR
				a.KODE_KARYAWAN	LIKE '%$key%' OR
				a.LAPOR_ID	LIKE '%$key%' OR
				a.PERB_TGL	LIKE '%$key%' OR
				a.PERB_WAKTU LIKE '%$key%')";
	}
	if ($where) $sql .= $key? " AND $where " : " $where ";
	if ($groupby) $sql .= " GROUP BY $groupby ";
	if ($orderby) $sql .= " GROUP BY $orderby ";
	$result = mysql_query($sql);
	return !$result? mysql_error() : $result;
}

function perb_select_corelate($key=false, $where=false, $orderby=false, $groupby=false){
	$sql = "SELECT * FROM perbuatan as a, detil_perbuatan as b
			WHERE a.PERB_NO = b.PERB_NO ";
	if ($key){
		$sql .= " AND (
				a.PERB_NO		LIKE '%$key%' OR
				a.KODE_KARYAWAN	LIKE '%$key%' OR
				a.LAPOR_ID		LIKE '%$key%' OR
				a.PERB_TGL		LIKE '%$key%' OR
				a.PERB_WAKTU 	LIKE '%$key%' OR
				b.DEPERB_NO		LIKE '%$key%' OR				
				b.PERB_NO		LIKE '%$key%' OR				
				b.DETACHIEV_ID 	LIKE '%$key%' OR				
				b.DEPERB_TEMPAT LIKE '%$key%' OR				
				b.DEPERB_TGL 	LIKE '%$key%' OR				
				b.DEPERB_WAKTU 	LIKE '%$key%' OR				
				b.DEPERB_COUNT 	LIKE '%$key%' OR				
				b.DEPERB_STATUS LIKE '%$key%') ";
	}
	if ($where) $sql .= " AND $where ";
	if ($groupby) $sql .= " GROUP BY $groupby ";
	if ($orderby) $sql .= " ORDER BY $orderby ";

	$result = mysql_query($sql);
	return !$result? mysql_error() : $result;
}

function perb_select_corelate2($key=false, $where=false, $orderby=false, $groupby=false){
	$sql = "SELECT a.PERB_NO, b.DEPERB_NO, a.PERB_TGL, a.PERB_WAKTU, f.NIK, f.LAPOR_NAMA, b.DEPERB_TGL, 
				b.DEPERB_WAKTU, a.KODE_KARYAWAN, c.NAMA_KARYAWAN, d.ACHIEV_BIDANG, d.ACHIEV_JENIS, 
				e.DETACHIEV_TINGKAT, e.DETACHIEV_ID, e.DETACHIEV_NAMA, b.DEPERB_STATUS, b.DEPERB_DESC, b.DEPERB_COUNT, 
				a.PERB_PRIVILEGES, a.PERB_POINT
			FROM perbuatan as a, 
				 detil_perbuatan as b, 
				 data_karyawan as c, 
				 prestasipelanggaran as d, 
				 detil_prestasi_pelanggaran as e,
				 pelapor as f
			WHERE a.PERB_NO = b.PERB_NO AND
				 a.KODE_KARYAWAN = c.KODE_KARYAWAN AND
				 a.LAPOR_ID = f.LAPOR_ID AND
				 b.DETACHIEV_ID = e.DETACHIEV_ID AND
				 d.ACHIEV_ID = e.ACHIEV_ID";
	if ($key){
		$sql .= " AND (
				a.PERB_TGL LIKE '%$key%' OR 
				a.PERB_WAKTU LIKE '%$key%' OR 
				f.NIK LIKE '%$key%' OR
				f.LAPOR_NAMA LIKE '%$key%' OR
				b.DEPERB_TGL LIKE '%$key%' OR
				b.DEPERB_WAKTU LIKE '%$key%' OR
				a.KODE_KARYAWAN LIKE '%$key%' OR
				c.NAMA_KARYAWAN LIKE '%$key%' OR
				d.ACHIEV_BIDANG LIKE '%$key%' OR
				d.ACHIEV_JENIS LIKE '%$key%' OR
				e.DETACHIEV_TINGKAT LIKE '%$key%' OR
				e.DETACHIEV_NAMA LIKE '%$key%' OR
				b.DEPERB_STATUS LIKE '%$key%') ";
	}
	if ($where) $sql .= " AND $where ";
	if ($groupby) $sql .= " GROUP BY a.PERB_NO, b.DEPERB_NO, $groupby ";
	if ($orderby) $sql .= " ORDER BY $orderby ";

	$result = mysql_query($sql);
	return !$result? mysql_error() : $result;	
}

function perb_delete($perbNO){
	$sql = "DELETE FROM perbuatan WHERE PERB_NO='$perbNO'";
	return mysql_query($sql);
}